import { Component } from '@angular/core';
import { Student } from './model/student';
import { StudentService } from './service/student.service';
import { StudentVo } from './vo/student-vo';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {z
  title = '云南民族大学';
  //变量的声明
  textvalue:string;
  numbervalue:number;

  student:Student;
  sl:Array<Student>;
  studentvo:StudentVo;

  constructor(private studentservice:StudentService){

  }

  //初始化方法
  ngOnInit() {
    //变量的初始化
    //this.textvalue='今天的天气很一般啊';
    //this.numbervalue=58+75;
    this.studentvo=new StudentVo();

    this.student = new Student();
    // this.student.name = "测试姓名";
    // this.student.sex = "男";
    // this.student.stunum = "201621447147";
    // this.student.birthday = new Date().getTime();
    // this.student.nativeplace ="测试籍贯";
    this.sl = new Array();

    this.loadStudentList();
  }


  //添加事件

  add(){
    console.dir(this.studentvo);

    this.studentservice.addStudentInfo(this.studentvo)
    .then((data:any)=>{
      //请求事件完成后
      this.loadStudentList();
    
    })
  }


  //从服务器端加载多行数据
  
  loadStudentList(){

    this.studentservice.getStudentList()
    .then((data:any)=>{
      this.sl = data;
    })
  }



  //通过对象的主键获取单个Student对象
  getToUpdateStudent(id:string){

    this.studentservice.getSingleStudentById(id)
    .then((data:Student)=>{
      this.studentvo=new StudentVo();
      this.studentvo.studentid=data.studentid;
      this.studentvo.xh=data.stunum;
      this.studentvo.xm=data.name;
    })
  }


  //删除单个对象

  delteStudent(id:string){
    this.studentservice.deleteStudent(id)
    .then((flag:boolean)=>{
      if(flag==true){
        this.loadStudentList();
      }else{
        alert("操作异常，请刷新后重试");
      }
    })
  }


}
